package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.validator.ValidatorAdd;
import com.heima.model.common.validator.ValidatorUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @作者 itcast
 * @创建日期 2021/11/17 11:39
 **/
@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理controller",tags = "频道管理controller")
public class AdChannelController {



    @Autowired
    AdChannelService adChannelServicel;
    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(adChannelServicel.getById(id));
    }
    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = adChannelServicel.list();
        ResponseResult responseResult = ResponseResult.okResult(list);
        responseResult.setErrorMessage("现在我想用jenkins自动构建任务");
        return responseResult;
    }

    @ApiOperation(value = "根据条件分页查询频道信息",notes = "按照name模糊分页查询，结果按照ord升序排序")
    @PostMapping("list")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDTO dto){
        return adChannelServicel.findByNameAndPage(dto);
    }


    @ApiOperation("保存频道信息")
    @PostMapping("save")
    public ResponseResult save(@RequestBody @Validated(ValidatorAdd.class) AdChannel channel){
        return adChannelServicel.insert(channel);
    }

    @ApiOperation("修改频道信息")
    @PostMapping("update")
    public ResponseResult update(@RequestBody @Validated(ValidatorUpdate.class) AdChannel channel){
        return adChannelServicel.update(channel);
    }

    @ApiOperation("删除频道信息")
    @GetMapping("del/{id}")
    public ResponseResult delete(@PathVariable Integer id) {
        return adChannelServicel.delete(id);
    }
}
